package com.hckj.jianzhijia;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.CountDownTimer;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.GetCallback;
import com.hckj.jianzhijia.model.City;
import com.hckj.jianzhijia.model.CompanyUser;
import com.hckj.jianzhijia.model.Province;
import com.hckj.jianzhijia.model.Region;
import com.hckj.jianzhijia.model.VersionUpdate;
import com.hckj.jianzhijia.services.APKLoadingService;
import com.hckj.jianzhijia.utils.ExceptionUtils;
import com.hckj.jianzhijia.utils.MyActivityManager;
import com.hckj.jianzhijia.utils.SharedPreferencesUtil;
import com.hckj.jianzhijia.utils.ToastUtils;
import com.hckj.jianzhijia.utils.UpdateManager;
import com.rey.material.app.Dialog;
import com.rey.material.app.DialogFragment;
import com.rey.material.app.SimpleDialog;

import java.util.List;

public class LoadingActivity extends BaseActivity {

    private SharedPreferencesUtil sharedPreferencesUtil;
    private boolean isLogin = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        sharedPreferencesUtil = new SharedPreferencesUtil(this);
        new CountDownTimer(1000, 1000) {

            @Override
            public void onFinish() {
                LoadingNetTask loadingNetTask = new LoadingNetTask();
                loadingNetTask.execute();
            }

            @Override
            public void onTick(long millisUntilFinished) {

            }
        }.start();

    }

    private class LoadingNetTask extends AsyncTask<Void, Void, String> {


        public LoadingNetTask() {
            super();
        }

        @Override
        protected String doInBackground(Void... params) {

            if (sharedPreferencesUtil.isAutoLogin()) {
                String userPhone = sharedPreferencesUtil.getCurrentUserPhone();
                String userPassword = sharedPreferencesUtil.getCurrentUserPassword();
                AVUser avUser;
                try {
                    avUser = AVUser.logIn(userPhone, userPassword);
                    AVObject avObject = avUser.getAVObject("userInfo");
                    avObject.fetchIfNeeded();
                    CompanyUser companyUser = new CompanyUser(avUser);
                    jianzhijiaApplacation.setCompanyUser(companyUser);
                    AVQuery provinceQuery = new AVQuery(Province.TABLE_NAME);
                    AVQuery cityQuery = new AVQuery(City.TABLE_NAME);
                    AVQuery regionQuery = new AVQuery(Region.TABLE_NAME);
                    provinceQuery.whereEqualTo("provinceId", jianzhijiaApplacation.getCompanyUser().getProvinceId());
                    cityQuery.whereEqualTo("cityId",jianzhijiaApplacation.getCompanyUser().getCityId());
                    regionQuery.whereEqualTo("regionId", jianzhijiaApplacation.getCompanyUser().getRegionId());

                    List provinceList =  provinceQuery.find();
                    List cityList =  cityQuery.find();
                    List regionList =  regionQuery.find();

                    if(provinceList.size()!=0){
                        jianzhijiaApplacation.getCompanyUser().setProvinceName(new Province((AVObject)(provinceList.get(0))).getProvince());
                    }

                    if(cityList.size()!=0){
                        jianzhijiaApplacation.getCompanyUser().setCityName(new City((AVObject) (cityList.get(0))).getCity());
                    }

                    if(regionList.size()!=0){
                        jianzhijiaApplacation.getCompanyUser().setRegionName(new Region((AVObject) (regionList.get(0))).getRegion());
                    }

                    isLogin = true;//待会直接进入登录界面
                } catch (AVException e) {

                    if (e.getCode() == 0) {
                        return "网络异常";
                    } else {
                        sharedPreferencesUtil.setAutoLogin(false);
                        sharedPreferencesUtil.setCurrentUserPhonePassword(userPhone, "");
                    }
                }
            }
            return null;
        }


        @Override
        protected void onPostExecute(String Void) {

            if (Void != null) {
                ToastUtils.showToast(getApplicationContext(), Void);
            }
            if (!isLogin) {
                startActivity(new Intent(getApplicationContext(), LoginActivity.class));
                finish();
            } else {
                startActivity(new Intent(getApplicationContext(), MainActivity.class));
                finish();
            }

            super.onPostExecute(Void);
        }
    }
}
